<template>
  <el-dialog width="800px" v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item :label="$t('channel.create')"> {{dataForm.createName}} </el-form-item>
      <el-form-item :label="$t('channel.category')">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="0">{{ $t("channel.category0") }}</el-radio>
          <el-radio :label="1">{{ $t("channel.category1") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('channel.categoryName')">
        <select-source v-model="dataForm.source"></select-source>
      </el-form-item>
      <el-form-item prop="level" :label="$t('channel.level')">
        <el-rate style="margin-top: 10px" v-model="dataForm.level"></el-rate>
      </el-form-item>
      <el-form-item prop="description" :label="$t('channel.remark')">
        <el-input v-model="dataForm.description" type="textarea" :placeholder="$t('channel.remarkOptions.tip')"></el-input>
      </el-form-item>
      <el-form-item prop="status" :label="$t('channel.switchStatus')">
        <el-switch v-model="dataForm.status"> </el-switch>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { UserInfo } from "@/constants/cacheKey";
import { getCache } from "@/utils/cache";
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
// import selectSource from "./select-source.vue";
export default defineComponent({
  // components: { selectSource },
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        status: 1,
        level: 0,
        pid: 0,
        createId: "",
        createName:'',
        subCount: 0,
        thirdCount: 0,
        title: "",
        type: "",
        description: "",
        source: []
      }
    });
  },
  computed: {
    dataRule() {
      return {
        type: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        status: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        level: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);

  },
  mounted(){
    var userInfo = getCache(UserInfo);
    debugger
    this.dataForm.createName = userInfo.realName
    this.dataForm.createId = userInfo.id
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get(`/demo/news/${this.dataForm.id}`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/demo/news", this.dataForm, {
          "content-type": "application/x-www-form-urlencoded"
        }).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
